var MainStageLayer = cc.Layer.extend({
    isMouseDown:false,

    ccbNode:null,

    attrDrawingSprite:null,

    teamRects:[],

    availableRects:[],

    rectsInited:false,

    init:function () {

        //////////////////////////////
        // 1. super init first
        this._super();
        this.setTouchEnabled(true);
        /////////////////////////////
        // 2. add a menu item with "X" image, which is clicked to quit the program
        //    you may modify it.
        // ask director the window size
        var size = cc.Director.getInstance().getVisibleSize();
        var size2 =  cc.Director.getInstance().getWinSize();

        cc.BuilderReader.setResourcePath("ccb/");

        //load ccbi scene
        this.ccbNode = cc.BuilderReader.load("ccb/MainMenuScene.ccbi", this);
        //this.ccbNode.animationManager.setCompletedAnimationCallback(this, this.animationCompleteCallback);

        //init scene
        //read record
        /*
            back 2
            button layer 10
                army 1
                hero 2
                shop 3
                boss 4
            stage layer 20
                stage 1-5 (left buttom, center, right, top, right top)
            role layer 30
                image 1
                name 2
                coin 3
                star 4
        */

        // 设置头像
        var roleImg=this.ccbNode.getChildByTag(30).getChildByTag(1);
        var nameLabel=this.ccbNode.getChildByTag(30).getChildByTag(2);
        var goldLabel=this.ccbNode.getChildByTag(30).getChildByTag(3);
        var starLabel=this.ccbNode.getChildByTag(30).getChildByTag(4);
        var roleImgPathTexture = cc.TextureCache.getInstance().addImage("character/"+Game.gameRecord[Game.currRecord]["character"][0][1]+".png");
        roleImg.setTexture(roleImgPathTexture);
        nameLabel.setString(Game.gameRecord[Game.currRecord]["player"]["name"]);
        goldLabel.setString(Game.gameRecord[Game.currRecord]["player"]["gold"]);
        starLabel.setString(Game.gameRecord[Game.currRecord]["player"]["star"]);

        // 初始化关卡状态
        for(var i=1;i<=Game.stageNum;i++){
            var stageStar=Game.gameRecord[Game.currRecord]["stage"][i];
            var stageSprite=this.ccbNode.getChildByTag(20).getChildByTag(i);
            if(stageStar==-1){
                // 未解锁
                var texture = cc.TextureCache.getInstance().addImage("ccb/ccbResources/stage_locked.png");
                stageSprite.setTexture(texture);
            }else if(stageStar==10){
                // 已通关
                var texture = cc.TextureCache.getInstance().addImage("ccb/ccbResources/stage_clear.png");
                stageSprite.setTexture(texture);
            }else{
                // 通关中
                var texture = cc.TextureCache.getInstance().addImage("ccb/ccbResources/stage_next.png");
                stageSprite.setTexture(texture);
            }
        }

        if(this.ccbNode != null) {
            this.addChild(this.ccbNode);
        }

        this.initTouchPositions();
    },
    animationCompleteCallback: function(){
        console.log("animation end...");
    },

    backBtnRect:null,
    armyBtnRect:null,
    heroBtnRect:null,
    shopBtnRect:null,
    bossBtnRect:null,
    stage1Rect:null, // left
    stage2Rect:null, // center
    stage3Rect:null, // right
    stage4Rect:null, // top
    stage5Rect:null, // right top
    initTouchPositions: function(){
        console.log("init Touch Positions");
        if(!this.rectsInited){
            //init rects
            this.backBtnRect=this.ccbNode.getChildByTag(2).getBoundingBox();

            this.armyBtnRect=this.ccbNode.getChildByTag(10).getChildByTag(1).getBoundingBox();
            this.heroBtnRect=this.ccbNode.getChildByTag(10).getChildByTag(2).getBoundingBox();
            this.shopBtnRect=this.ccbNode.getChildByTag(10).getChildByTag(3).getBoundingBox();
            this.bossBtnRect=this.ccbNode.getChildByTag(10).getChildByTag(4).getBoundingBox();
            
            this.stage1Rect=this.ccbNode.getChildByTag(20).getChildByTag(1).getBoundingBox();
            this.stage2Rect=this.ccbNode.getChildByTag(20).getChildByTag(2).getBoundingBox();
            this.stage3Rect=this.ccbNode.getChildByTag(20).getChildByTag(3).getBoundingBox();
            this.stage4Rect=this.ccbNode.getChildByTag(20).getChildByTag(4).getBoundingBox();
            this.stage5Rect=this.ccbNode.getChildByTag(20).getChildByTag(5).getBoundingBox();

            this.rectsInited=true;
        }
    },
    onTouchesEnded: function(pTouch, pEvent){
        var touchPoint=pTouch[0].getLocation();
        // touch on stage
        var pointAtStage=this.ccbNode.getChildByTag(20).convertToNodeSpace(touchPoint);
        if(cc.rectContainsPoint(this.stage1Rect, pointAtStage)){
            console.log("Touch on Stage1");
            return true;
        }
        if(cc.rectContainsPoint(this.stage2Rect, pointAtStage)){
            console.log("Touch on Stage2");
            return true;
        }
        if(cc.rectContainsPoint(this.stage3Rect, pointAtStage)){
            console.log("Touch on Stage3");
            return true;
        }
        if(cc.rectContainsPoint(this.stage4Rect, pointAtStage)){
            console.log("Touch on Stage4");
            return true;
        }
        if(cc.rectContainsPoint(this.stage5Rect, pointAtStage)){
            console.log("Touch on Stage5");
            return true;
        }
        // touch on menu
        var pointAtMenu=this.ccbNode.getChildByTag(10).convertToNodeSpace(touchPoint);
        if(cc.rectContainsPoint(this.armyBtnRect, pointAtMenu)){
            console.log("Touch on ARMY");
            Game.currScene=new TeamScene();
            cc.Director.getInstance().replaceScene(cc.TransitionFade.create(0.5, Game.currScene, cc.black()));
            return true;
        }
        if(cc.rectContainsPoint(this.heroBtnRect, pointAtMenu)){
            console.log("Touch on HERO");
            Game.currScene=new EquipSkillScene();
            cc.Director.getInstance().replaceScene(cc.TransitionFade.create(0.5, Game.currScene, cc.black()));
            return true;
        }
        if(cc.rectContainsPoint(this.shopBtnRect, pointAtMenu)){
            console.log("Touch on SHOP");
            Game.currScene=new ShopScene();
            cc.Director.getInstance().replaceScene(cc.TransitionFade.create(0.5, Game.currScene, cc.black()));
            return true;
        }
        if(cc.rectContainsPoint(this.bossBtnRect, pointAtMenu)){
            console.log("Touch on BOSS");
            return true;
        }
        // touch on back
        if(cc.rectContainsPoint(this.backBtnRect, touchPoint)){
            console.log("Touch BACK");
            Game.currScene=new StartScene();
            cc.Director.getInstance().replaceScene(cc.TransitionFade.create(0.5, Game.currScene, cc.black()));
            return true;
        }
    },
    onTouchesBegan: function(pTouch, pEvent){
        var touchPoint=pTouch[0].getLocation();
    },
    onTouchesMoved: function(pTouch, pEvent){
        var touchPoint=pTouch[0].getLocation();
    },
});

var MainStageScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new MainStageLayer();
        layer.init();
        this.addChild(layer);
    }
});